package com.czk.ssaprompttemplate.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-10-16
 * @Description:
 * @Version: 1.0
 */
@RestController
public class PromptTemplateController {


    @Resource(name = "deepseekChatClient")
    private ChatClient deepeekChatClient;
    @Resource(name = "qwenChatClient")
    private ChatClient qwenChatClient;
    @Resource(name = "deepseek")
    private ChatModel deepeekChatModel;
    @Resource(name = "qwen")
    private ChatModel qwenChatModel;

    /**
     * 占位符创建模板
     * 动态插入配置参数
     *
     * @return reactor.core.publisher.Flux<java.lang.String>
     * @Author chenzhangkun
     * @Description
     * @Date 21:31 2025/10/16
     * @Param [topic, output_format, wordCount]
     **/
    @GetMapping(value = "/prompt-template/chat")
    public Flux<String> getAnswer(@RequestParam(name = "topic", defaultValue = "篮球") String topic,
                                  @RequestParam(name = "output_format", defaultValue = "文本") String output_format,
                                  @RequestParam(name = "word_count", defaultValue = "300") String wordCount) {
        // 通过占位符创建模板
        PromptTemplate promptTemplate = new PromptTemplate(
                """
                        讲一个关于{topic}的故事，并以{output_format}格式输出，故事中至少包含{word_count}个单词。
                        """
        );
        Prompt prompt = promptTemplate.create(Map.of("topic", topic, "output_format", output_format, "word_count", wordCount));
        return deepeekChatClient.prompt(prompt).stream().content();
    }


    @Value("classpath:/prompttemplate/ball.txt")
    private org.springframework.core.io.Resource userTemplate;

    /**
     * 使用读取文件的形式获取占位符模板
     *
     * @param topic
     * @param output_format
     * @param wordCount
     * @return
     */
    @GetMapping(value = "/prompt-template/chat-file")
    public Flux<String> getAnswerByTemplate(@RequestParam(name = "topic", defaultValue = "篮球") String topic,
                                            @RequestParam(name = "output_format", defaultValue = "文本") String output_format,
                                            @RequestParam(name = "word_count", defaultValue = "300") String wordCount) {
        // 通过占位符创建模板
        PromptTemplate promptTemplate = new PromptTemplate(userTemplate);
        Prompt prompt = promptTemplate.create(Map.of("topic", topic, "output_format", output_format, "word_count", wordCount));
        return deepeekChatClient.prompt(prompt).stream().content();
    }

    /**
     * 通过占位符创建模板
     * 创建系统模板
     *
     * @return reactor.core.publisher.Flux<java.lang.String>
     * @Author chenzhangkun
     * @Description
     * @Date 21:53 2025/10/16
     * @Param [systemTopic, userTopic]
     **/
    @GetMapping(value = "/prompt-template/chat3")
    public Flux<String> chat3(@RequestParam(name = "topic", defaultValue = "篮球") String systemTopic,
                              @RequestParam(name = "userTopic", defaultValue = "湖人") String userTopic) {
        // 通过占位符创建模板
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate("你是一个{systemTopic}助手，你只回答{systemTopic}相关问题，其他无关{systemTopic}的问题只需要回复下面句子：Lets Go Lakers");
        Message sysPrompt = systemPromptTemplate.createMessage(Map.of("systemTopic", systemTopic));
        // 用户模板
        PromptTemplate userPromptTemplate = new PromptTemplate("详细介绍一下{userTopic}");
        Message userMessage = userPromptTemplate.createMessage(Map.of("userTopic", userTopic));
        Prompt prompt = new Prompt(List.of(sysPrompt, userMessage));

        return qwenChatModel.stream(prompt).mapNotNull(response -> response.getResults().getFirst().getOutput().getText());
    }
}
